package kin.controller.system;



import java.util.LinkedHashMap;



import java.util.Locale;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;









import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;







import kin.annotation.system.Module;
import kin.bean.system.Form;
import kin.bean.system.Result;
import kin.service.system.FormServiceManager;
import kin.system.util.ApplicationContextHelper;
import kin.system.util.JsonBuildClassAttribute;



@Controller
@Module(label="表管理",renderToNAV=true)
@RequestMapping("/form")
public class FormController {

	@Inject
	private FormServiceManager  formServiceManager;

	
	@ResponseBody
	@RequestMapping(value ="/findColumns", method = RequestMethod.GET)
    public  String findColumns(@RequestParam String className,HttpServletRequest request) {
		
		String str = null;
        	LinkedHashMap<String, Object> where=new LinkedHashMap<String, Object>();
        	where.put("form_name", className);
        	Form f;
			try {
				f = (Form)this.getFormServiceManager().find( where, null,null);
				str = JsonBuildClassAttribute.getColumn(f);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	
       	return str;

	}
	
	
	@ResponseBody
	@RequestMapping(value = "/findButtons", method = RequestMethod.GET)
	public String findButtons(@RequestParam String className){

		    
		 String str = null;
        	LinkedHashMap<String, Object> where=new LinkedHashMap<String, Object>();
        	where.put("form_name", className);
        	Form f;
			try {
				f = (Form)this.getFormServiceManager().find( where, null,null);
				str = JsonBuildClassAttribute.getButtons(f);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	
       	return str;

	}
	
	@ResponseBody
	@RequestMapping(value = "/findModels", method = RequestMethod.GET)
	public String findModels(@RequestParam String className) {
		System.out.println("ccc");
		/*LocalSessionFactoryBean ass=this.getFormManager().getLocalSessionFactoryBean();
		
		// LocalSessionFactoryBean ass = (LocalSessionFactoryBean) ApplicationContextHelper.getBean("&customerSessionFactory");
   	 
   	  SchemaExport ses = new SchemaExport(ass.getConfiguration());
   	 
   	  ses.create(true, true);*/
		
		
		System.out.println("------------model");
       
        	LinkedHashMap<String, Object> where=new LinkedHashMap<String, Object>();
        	where.put("form_name", className);
        	String str = null;
        	
        	where.put("form_name", className);
        	Form f;
			try {
				f = (Form)this.getFormServiceManager().find( where, null,null);
				str=JsonBuildClassAttribute.getModel(f);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        
        
        	return str;
      
        }
	

	@ResponseBody
	@RequestMapping(value = "/findForm", method = RequestMethod.GET)
	public String findForm(@RequestParam String className,@RequestParam int id,HttpServletRequest request) {
		// RequestContext requestContext =request;

		 Locale myLocale = request.getLocale();

		 System.out.println(myLocale);
        	LinkedHashMap<String, Object> where=new LinkedHashMap<String, Object>();
        	where.put("form_name", className);
        	String str=null;
        	try {
				 Form f = (Form)this.getFormServiceManager().find( where, null,null);
				//WebApplicationContext webApplicationContext = (WebApplicationContext) request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	        	str=JsonBuildClassAttribute.getForm(f,id);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        	
        	return str;
        	
        }
		
	@ResponseBody
	@Module(label="添加表",renderToNAV=true)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@RequestParam String className) {
		System.out.println("------------model");
       
        
        
        	return null;
        }
		
  
     
     @ResponseBody
     @Module(label="查看所有表",renderToNAV=true)
 	@RequestMapping(value = "/list", method = RequestMethod.GET)
     public Object list(){
    	 Result r=new Result();
    	 //this.getResult().setObject(formManager.findData(false, 0, 0, null, null));               customerSessionFactory
    	 //LocalSessionFactoryBean ass = (LocalSessionFactoryBean)ApplicationContextHelper.getBean("&sessionFactory");
         
         
         //lp.configure(props)
     
         //Configuration configuration= ass.getConfiguration();
         //configuration.configure();
      
         //configuration.setProperty("hibernate.hbm2ddl.auto","create");
        
         //configuration.configure();
     // SchemaExport ses = new SchemaExport( configuration);
      
     //ses.create(true, true);

    	 
    	
    	 return  r;
     }


	public FormServiceManager getFormServiceManager() {
		return formServiceManager;
	}


	public void setFormServiceManager(FormServiceManager formServiceManager) {
		this.formServiceManager = formServiceManager;
	}


	
	

}
